package com.mooc.ppjoke.exoplayer;

import android.app.Application;
import android.net.Uri;

import com.mooc.lib_common.MyApplication;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.FileDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSinkFactory;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.util.Util;

import java.util.HashMap;

/**
 * @ClassName: PagedListPlayerManager$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/22 19:33
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class PagedListPlayerManager {
    private static HashMap<String ,PageListPlay> playHashMap=new HashMap<>();
    static {
        Application application = MyApplication.getsApplication();
        DefaultHttpDataSourceFactory factory = new DefaultHttpDataSourceFactory(Util.getUserAgent(application, application.getPackageName()));
        Cache cache=new SimpleCache(application.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024*1024*200));
        CacheDataSinkFactory cacheDataSinkFactory = new CacheDataSinkFactory(cache, Long.MAX_VALUE);
        CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(cache, factory,new FileDataSourceFactory(),cacheDataSinkFactory,CacheDataSource.FLAG_BLOCK_ON_CACHE,null);
        mediaFactory = new ProgressiveMediaSource.Factory(cacheDataSourceFactory);


    }
    public static MediaSource createMediaSource(String url){
        return mediaFactory.createMediaSource(Uri.parse(url));
    }

    private static ProgressiveMediaSource.Factory mediaFactory;

    public static PageListPlay get(String name){
        PageListPlay pageListPlay = playHashMap.get(name);
        if(pageListPlay==null){
            pageListPlay=new PageListPlay();
            playHashMap.put(name, pageListPlay);
        }
        return pageListPlay;
    }

    public static void release(String name){
        PageListPlay pageListPlay = playHashMap.get(name);
        if(pageListPlay!=null){
            pageListPlay.release();
        }
    }
} 
